<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--<title>西窗烛</title>-->
</head>

<body style="overflow: hidden;">
    <webview id="xcz" src="http://lib.xcz.im/library" style="width: 1080px;height: 768px;"></webview>
</body>
<script>
    onload = () => {
        const {
            ipcRenderer,
            remote,
            clipboard,
        } = require('electron')
        var utterThis = new window.SpeechSynthesisUtterance();
        utterThis.onend = (e) => {
            ipcRenderer.send('speakStop')
        }
        utterThis.onerror = (e) => {
            ipcRenderer.send('speakStop')
        }
        utterThis.rate = 0.7
        utterThis.pitch = 0.6
        const robot = require('robotjs');
        var browser;
        const browserView = document.getElementById('xcz')
        browserView.addEventListener('load-commit', () => {
            browser = remote.webContents.fromId(browserView.getWebContentsId())
            browser.insertCSS('.market,.discover,.btn-log-in,.logo,.bottom-bar{display: none !important;}')
            browser.insertCSS('#footer{visibility:hidden !important;}')
            browser.insertCSS('* {font-family:STSong,STFangsong !important;}')
            browser.insertCSS('input::-webkit-input-placeholder{font-family:STSong,STFangSong !important;}')
            browser.executeJavaScript('var elements=document.getElementsByTagName("a");for(var i=0;i<elements.length;i++){elements[i].target="_self"}')
        });
        ipcRenderer.on('commander', (event, arg) => {
            if (arg === 'up') {
                robot.keyTap('pageup')
            } else if (arg === 'down') {
                robot.keyTap('pagedown')
            } else if (arg === 'left') {
                browser.executeJavaScript('var element=document.getElementsByClassName("prev-img");if(element.length > 0){element[0].click();0;}', true)
            } else if (arg === 'right') {
                browser.executeJavaScript('var element=document.getElementsByClassName("next-img");if(element.length > 0){element[0].click();0;}', true)
            } else if (arg === 'search') {
                browser.executeJavaScript('var element=document.getElementsByClassName("btn-search");if(element.length > 0){element[0].click();0;}', true)
            } else if (arg === 'copy') {
                browser.executeJavaScript('if(document.getElementsByClassName("work").length > 0){var e=document.getElementsByClassName("content indent");if(e.length==0){e=document.getElementsByClassName("content center")};var res="";var title=document.getElementsByClassName("title");if(title.length>0){res+=(title[0].textContent + "\\n")};var author = document.getElementsByClassName("author");if(author.length > 0){res += (author[0].textContent.trim() + "\\n")}; if(e.length > 0){var ee=e[0];var child=ee.children;for(var i=0;i<child.length;i++){res += (child[i].textContent + "\\n")}};res.trim();}else{"";}').then((res) => {
                    if (res.length > 0) {
                        clipboard.writeText(res)
                        res = res.split('\n')
                        var title = res[0].trim()
                        var author = res[1].trim()
                        var content = ''
                        for (var i = 2; i < res.length; i++) {
                            content += (res[i] + '\n')
                        }
                        ipcRenderer.send('copysuccess', title, author, content)
                    }
                }).catch((e) => {
                    ipcRenderer.send('copyfailed', e)
                });
            } else if (arg == 'startSpeak') {
                browser.executeJavaScript('if(document.getElementsByClassName("work").length > 0){var e=document.getElementsByClassName("content indent");if(e.length==0){e=document.getElementsByClassName("content center")};var res="";var title=document.getElementsByClassName("title");if(title.length>0){res+=(title[0].textContent + "\\n")};var author = document.getElementsByClassName("author");if(author.length > 0){res += (author[0].textContent.trim() + "\\n")}; if(e.length > 0){var ee=e[0];var child=ee.children;for(var i=0;i<child.length;i++){res += (child[i].textContent + "\\n")}};res.trim();}else{"";}').then((res) => {
                    if (res.length > 0) {
                        utterThis.text = res.replace(/[，/]/g, '，，，').replaceAll('\n', '，，,').replace(/\s*/g, '')
                        window.speechSynthesis.speak(utterThis);
                    } else {
                        ipcRenderer.send('speakStop')
                    }
                }).catch((e) => {
                    ipcRenderer.send('speakStop')
                    console.log(e)
                });
            } else if (arg == 'stopSpeak') {
                window.speechSynthesis.cancel()
                ipcRenderer.send('speakStop')
            }
        });
    }
</script>

</html>
